﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GameClient.Managers;
using Backend.Packets;

namespace GameClient
{
    public partial class Chat : Form
    {
        ClientManager m_Manager;
        JoinRoom m_JoinRoomForm;

        public Chat()
        {
            InitializeComponent();
            m_Manager = ClientManager.GetInstance();
            ChatPacket Payload = new ChatPacket();
            Payload.Command = ChatPacket.ChatCommand.JOIN_ROOM;
            Payload.Room = "Jeff's Room";

            m_Manager.Send(Payload);

            Payload = new ChatPacket();
            Payload.Command = ChatPacket.ChatCommand.LIST_ROOM;
            Payload.Room = "Jeff's Room";

            m_Manager.Send(Payload);

            m_JoinRoomForm = new JoinRoom();

            m_Manager.ChatMgr.ChatServerMessage += new Action<string>(ChatMgr_ChatServerMessage);
            m_Manager.ChatMgr.RoomList += new Action<string>(ChatMgr_RoomList);
            m_Manager.ChatMgr.RoomMessage += new Action<string, string>(ChatMgr_RoomMessage);
            m_Manager.ChatMgr.UserList += new Action<string>(ChatMgr_UserList);
            m_Manager.ChatMgr.UserMessage += new Action<string, string>(ChatMgr_UserMessage);
        }

        void ChatMgr_UserMessage(string arg1, string arg2)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string, string>(ChatMgr_UserMessage), arg1, arg2);
                return;
            }

            PrivateMessage NewMessage = new PrivateMessage(arg1, arg2);
            if (NewMessage.ShowDialog() == DialogResult.OK)
            {
                m_Manager.ChatMgr.MessageUser(arg1, NewMessage.Message);
            }
        }

        void ChatMgr_UserList(string obj)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(ChatMgr_UserList), obj);
                return;
            }

            listBox1.Items.Clear();

            string[] sUsers = obj.Split(',');

            foreach (string s in sUsers)
                listBox1.Items.Add(s);
        }

        void ChatMgr_RoomMessage(string arg1, string arg2)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string, string>(ChatMgr_RoomMessage), arg1, arg2);
                return;
            }

            textBox1.Text += arg1 + " : " + arg2 + "\r\n";
        }

        void ChatMgr_RoomList(string obj)
        {
            throw new NotImplementedException();
        }

        void ChatMgr_ChatServerMessage(string obj)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(ChatMgr_ChatServerMessage), obj);
                return;
            }

            textBox1.Text += obj;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_Manager.ChatMgr.MessageRoom(textBox2.Text);

            textBox2.Text = "";
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Return Key
            if (e.KeyChar == 13)
                button1_Click(null, null);
        }

        private void joinRoomToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
                return;
            try
            {

                SendMessage PrivateMessage = new SendMessage();
                if (PrivateMessage.ShowDialog() == DialogResult.OK)
                {
                    m_Manager.ChatMgr.MessageUser(((string)listBox1.SelectedItem), PrivateMessage.Message);
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.StackTrace);
            }
        }
    }
}
